<?php

namespace App\Admin\Controllers\Shop;

use App\Models\Shop\Good;
use App\Models\Shop\GoodsClass;
use App\Models\Shop\GoodsSku;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class GoodsController extends AdminController
{

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\Shop\Good';

    protected $switchArr = [
        [
            'on' => ['value'=>1,'text'=>'已上架','color'=>'success' ],
            'off' => ['value'=>0,'text'=>'已下架','color'=>'primary' ],
        ]
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Good());

        $grid->column('id', __('field.id'));
        $grid->column('name', __('field.name'));
        $grid->column('class_id', __('field.class_id'));
        $grid->column('original_price', __('field.original_price'));
        $grid->column('sell_price', __('field.sell_price'));
        $grid->column('sell_number', __('field.sell_number'));
        $grid->column('info_images', __('field.info_images'));
        $grid->column('loop_images', __('field.loop_images'));
        $grid->column('cover_img', __('field.cover_img'));
        $grid->column('is_assemble', __('field.is_assemble'));
        $grid->column('start_time', __('field.start_time'));
        $grid->column('ent_time', __('field.ent_time'));
        $grid->column('on_sale', __('field.on_sale'))->switch($this->switchArr);
        $grid->column('attribute_list', __('field.attribute_list'));
        $grid->column('created_at', __('field.created_at'));
        $grid->column('updated_at', __('field.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Good::findOrFail($id));

        $show->field('id', __('field.id'));
        $show->field('name', __('field.name'));
        $show->field('class_id', __('field.class_id'));
        $show->field('original_price', __('field.original_price'));
        $show->field('sell_price', __('field.sell_price'));
        $show->field('sell_number', __('field.init_sell_number'));
        $show->field('info_images', __('field.info_images'));
        $show->field('loop_images', __('field.loop_images'));
        $show->field('cover_img', __('field.cover_img'));
        $show->field('is_assemble', __('field.is_assemble'));
        $show->field('start_time', __('field.start_time'));
        $show->field('ent_time', __('field.ent_time'));
        $show->field('attribute_list', __('field.attribute_list'));
        $show->field('created_at', __('field.created_at'));
        $show->field('updated_at', __('field.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Good());

        $form->column(1/4, function ($form){
            $form->text('name', __('field.name'));
            $form->select('class_id', __('field.class_id'))
                ->options((new GoodsClass())->get(['id','name'])
                ->pluck('name','id'));
            $form->number('sell_number', __('field.init_sell_number'));
            $form->number('is_assemble', __('field.is_assemble'))->default(2);
            $form->datetime('start_time', __('field.start_time'));
            $form->datetime('ent_time', __('field.ent_time'));
            $form->image('cover_img', __('field.cover_img'))->uniqueName();

        });
        $form->column(3/4,function ($form){
            /*$form->table('attribute_list', __('field.attribute_list'), function ($table) {
                $table->text('key');
                $table->text('value');
                $table->text('desc');
            });*/

            $form->sku('sku','商品SKU');
            $form->multipleImage('info_images', __('field.info_images'))->sortable()->removable()->uniqueName();
            $form->multipleImage('loop_images', __('field.loop_images'))->sortable()->removable()->uniqueName();
        });
//        $form->ignore(['sku']);
        $form->saving(function (Form $form){
//            $form = $form->removeIgnoredFields(['sku']);
//            $form::forget('sku') ;
//            dd($form->sku);
            return $form;
        });
        $form->saved(function (Form $form){
            $sku = $form->sku;
            $goods_id = $form->model()->id;
            (new GoodsSku())->saveSku($sku, $goods_id);
            return $form;
        });



        return $form;
    }
}
